﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using UBCon.Core;
using UBCon.Core.Commands;
using System.Windows.Media;

namespace UBConDemo.CustomControls
{
    public class UbConItemsControl:Selector
    {
        //------------------------------------------------------
        //
        //  Constructors
        //
        //------------------------------------------------------
        #region Constructors

        /// <summary>
        /// Default static constructor.
        /// </summary>
        static UbConItemsControl() 
        {
            Type ownerType = typeof(UbConItemsControl);

            DefaultStyleKeyProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(ownerType));

            
        }

        /// <summary>
        /// Initializes a new instance of UBConDemo.CustomControls.UbConItemsControl class.
        /// </summary>
        public UbConItemsControl():base() 
        {
            BackCommand = new Command(OnBackCommandExecuted);
        }
        #endregion

        //------------------------------------------------------
        //
        //  Properties
        //
        //------------------------------------------------------
        #region Properties

        /// <summary>
        /// Gets or sets horizontal offset.
        /// </summary>
        public double HorizontalOffset
        {
            get { return (double)GetValue(HorizontalOffsetProperty); }
            set { SetValue(HorizontalOffsetProperty, value); }
        }

        /// <summary>
        /// Identifies UBConDemo.CustomControls.UbConItemsControl.HorizontalOffset dependency property.
        /// </summary>
        public static readonly DependencyProperty HorizontalOffsetProperty = WrapPanel.HorizontalOffsetProperty.AddOwner(typeof(UbConItemsControl));

        /// <summary>
        /// Gets or sets overall width of panel.
        /// </summary>
        public double ExtentWidth
        {
            get { return (double)GetValue(ExtentWidthProperty); }
            set { SetValue(ExtentWidthProperty, value); }
        }

        /// <summary>
        /// Identifies UBConDemo.CustomControls.UbConItemsControl.ExtentWidth dependency property.
        /// </summary>
        public static readonly DependencyProperty ExtentWidthProperty = WrapPanel.ExtentWidthProperty.AddOwner(typeof(UbConItemsControl));

        /// <summary>
        /// Gets or sets viewport's width.
        /// </summary>
        public double ViewportWidth
        {
            get { return (double)GetValue(ViewportWidthProperty); }
            set { SetValue(ViewportWidthProperty, value); }
        }

        /// <summary>
        /// Identifies UBConDemo.CustomControls.UbConItemsControl.ViewportWidth dependency property.
        /// </summary>
        public static readonly DependencyProperty ViewportWidthProperty = WrapPanel.ViewportWidthProperty.AddOwner(typeof(UbConItemsControl));

        /// <summary>
        /// Gets or sets a root control that makes selected item's view visible.
        /// </summary>
        public UIElement ContentViewer
        {
            get { return (UIElement)GetValue(ContentViewerProperty); }
            set { SetValue(ContentViewerProperty, value); }
        }

        /// <summary>
        /// Identifies UBConDemo.CustomControls.UbConItemsControl.ViewportWidth dependency property.
        /// </summary>
        public static readonly DependencyProperty ContentViewerProperty =
            DependencyProperty.Register("ContentViewer", typeof(UIElement), typeof(UbConItemsControl), new PropertyMetadata(null));

        /// <summary>
        /// Gets or sets content that holds selected item's view.
        /// </summary>
        public ContentPresenter ContentPresenter
        {
            get { return (ContentPresenter)GetValue(ContentPresenterProperty); }
            set { SetValue(ContentPresenterProperty, value); }
        }

        /// <summary>
        /// Identifies UBConDemo.CustomControls.UbConItemsControl.ContentPresenter dependency property.
        /// </summary>
        public static readonly DependencyProperty ContentPresenterProperty =
            DependencyProperty.Register("ContentPresenter", typeof(ContentPresenter), typeof(UbConItemsControl));

        /// <summary>
        /// Gets or sets a command that closes content presenter.
        /// </summary>
        public Command BackCommand
        {
            get { return (Command)GetValue(BackCommandProperty); }
            set { SetValue(BackCommandProperty, value); }
        }

        /// <summary>
        /// Identifies UBConDemo.CustomControls.UbConItemsControl.BackCommand dependency property.
        /// </summary>
        public static readonly DependencyProperty BackCommandProperty =
            DependencyProperty.Register("BackCommand", typeof(Command), typeof(UbConItemsControl));

        
        #endregion

        //------------------------------------------------------
        //
        //  Depenency Property Changed Callbacks
        //
        //------------------------------------------------------
        #region Depenency Property Changed Callbacks
        #endregion

        //------------------------------------------------------
        //
        //  Overrides
        //
        //------------------------------------------------------
        #region Overrides
       
        /// <summary>
        ///Creates or identifies the element that is used to display the given item.
        /// </summary>
        /// <returns>Type: System.Windows.DependencyObject. The element that is used to display the given item.</returns>
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new UbConItem();
        }
        /// <summary>
        /// Determines if the specified item is (or is eligible to be) its own ItemContainer. 
        /// (Overrides ItemsControl.IsItemItsOwnContainerOverride(Object).)
        /// </summary>
        /// <param name="item">Specified item.</param>
        /// <returns>true if the item is its own ItemContainer; otherwise, false.</returns>
        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            bool isValidItem = item is UbConItem;

            if (isValidItem)
            {
                //do here custom logic
            }
            return isValidItem;
        }
        #endregion

        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------
        #region Public Methods
        #endregion

        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------
        #region Internal Methods

        /// <summary>
        /// Shows view when selection changes.
        /// </summary>
        internal void ShowView() 
        {
            if (ContentViewer != null && ContentPresenter != null)
            {
                if (ShowContentAnimation == null)
                {
                    BuilShowContentAnimation();
                }
                ContentViewer.Visibility = System.Windows.Visibility.Visible;
                ShowContentAnimation.Begin();
            }
        }

        #endregion

        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------
        #region Private Methods

        /// <summary>
        /// Invoked when BackCommand is executed.
        /// </summary>
        private void OnBackCommandExecuted(object parameter) 
        {
            if (ContentViewer != null && ContentPresenter != null) 
            {
                ContentViewer.Visibility = System.Windows.Visibility.Collapsed;
                (ContentViewer.RenderTransform as TranslateTransform).X = -18.0;
                ClearValue(SelectedItemProperty);
            }
        }

        /// <summary>
        /// Initializes a new instance of System.Windows.Media.Animation.Storyboard that shows content of selected item.
        /// </summary>
        private void BuilShowContentAnimation()
        {
            DoubleAnimation da = new DoubleAnimation()
            {
                EasingFunction = new ExponentialEase()
                {
                    EasingMode = EasingMode.EaseOut,
                    Exponent = 5
                }
            };

            Storyboard.SetTarget(da, this.ContentViewer);
            Storyboard.SetTargetProperty(da, new PropertyPath(Constants.XPropertyPath));
            da.From = -18.0D;
            da.To = 0.0;
            da.Duration = new Duration(TimeSpan.FromSeconds(0.75));
            ShowContentAnimation = new Storyboard();
            ShowContentAnimation.Children.Add(da);
        }


        #endregion

        //------------------------------------------------------
        //
        //  Event Handlers
        //
        //------------------------------------------------------
        #region Event Handlers
        #endregion

        //------------------------------------------------------
        //
        //  Fields
        //
        //------------------------------------------------------
        #region Fields

        private Storyboard ShowContentAnimation;

        #endregion

        //------------------------------------------------------
        //
        //  Events
        //
        //------------------------------------------------------
        #region Events
        #endregion

        //------------------------------------------------------
        //
        //  Commands
        //
        //------------------------------------------------------
        #region Commands
        #endregion

    }
}
